

import formula.calculator.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TemperatureConversion extends Activity{

        private TableLayout tl;
        private TableRow row;
        private TextView tv;
        private double finalFahrenheit;
        private double finalCelsius;
        private double finalKelvin;
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperature_conversion_activity);
        tl = (TableLayout)findViewById(R.id.TableLayout1);
        row = new TableRow(this);
        tv = new TextView(this);
    }
        
        private EditText fahrenheit;
        private String sFahrenheit;
        private EditText celsius;
        private String sCelsius;
        private EditText kelvin;
        private String sKelvin;
        
        public void onClick(View v) {
                switch(v.getId()) {
                        case R.id.tempConvert:
                                convertTemp();
                                break;
                }
        }
        
        private void convertTemp() {
                fahrenheit = (EditText) findViewById(R.id.fahrenheitValue);
                celsius = (EditText) findViewById(R.id.celsiusValue);
                kelvin = (EditText) findViewById(R.id.kelvinValue);
                sFahrenheit = fahrenheit.getText().toString();
                sCelsius = celsius.getText().toString();
                sKelvin = kelvin.getText().toString();
                double fahrenheitNum = 0;
                double celsiusNum = 0;
                double kelvinNum = 0;
                if(!sFahrenheit.equals("")) {
                        fahrenheitNum = Double.parseDouble(sFahrenheit);
                }
                if(!sCelsius.equals("")) {
                        celsiusNum = Double.parseDouble(sCelsius);
                }
                if(!sKelvin.equals("")) {
                        kelvinNum = Double.parseDouble(sKelvin);
                }

        if(sFahrenheit.equals("") && !sCelsius.equals("")) {
                finalFahrenheit = CelsiusToFahrenheit(celsiusNum);
                finalCelsius = celsiusNum;
                finalKelvin = CelsiusToKelvin(celsiusNum);
        } else if(sFahrenheit.equals("") && !sKelvin.equals("")) {
                finalFahrenheit = KelvinToFahrenheit(kelvinNum);
                finalCelsius = KelvinToCelsius(kelvinNum);
                finalKelvin = kelvinNum;
        } else if(sCelsius.equals("") && !sFahrenheit.equals("")) {
                finalFahrenheit = fahrenheitNum;
                finalCelsius = FahrenheitToCelsius(fahrenheitNum);
                finalKelvin = FahrenheitToKelvin(fahrenheitNum);
        } else if(sCelsius.equals("") && !sKelvin.equals("")) {
                finalFahrenheit = KelvinToFahrenheit(kelvinNum);
                finalCelsius = KelvinToCelsius(kelvinNum);
                finalKelvin = kelvinNum;
        } else if(sKelvin.equals("") && !sFahrenheit.equals("")) {
                finalFahrenheit = fahrenheitNum;
                finalCelsius = FahrenheitToCelsius(fahrenheitNum);
                finalKelvin = FahrenheitToKelvin(fahrenheitNum);
        } else if(sKelvin.equals("") && !sCelsius.equals("")) {
                finalFahrenheit = CelsiusToFahrenheit(celsiusNum);
                finalCelsius = celsiusNum;
                finalKelvin = CelsiusToKelvin(celsiusNum);
        }
        
        tv.setText("Fahrenheit: " + finalFahrenheit + "Celsius: " + finalCelsius + "Kelvin: " + finalKelvin);
        tl.addView(row);
        row.addView(tv);
        }

/****                   Temperature Conversions                         ****/
        
        private double FahrenheitToCelsius(double fahrenheit) {
                double result = (fahrenheit - 32) * 5.0 / 9;
                return result;
        }
        
        private double CelsiusToFahrenheit(double celsius) {
                double result = 9.0 / 5 * celsius + 32;
                return result;
        }
        
        private double FahrenheitToKelvin(double fahrenheit) {
                double result = ((fahrenheit - 32) * 5 / 9) + 273.15;
                return result;
        }
        
        private double CelsiusToKelvin(double celsius) {
                double result = celsius + 273.15;
                return result;
        }
        
        private double KelvinToCelsius(double kelvin) {
                double result = kelvin - 273.15;
                return result;
        }
        
        private double KelvinToFahrenheit(double kelvin) {
                double result = 9.0 / 5 * (kelvin - 273.15)  + 32;
                return result;
        }

}